let SZLogic = require('./SZLogic');

cc.Class({
	extends: cc.Component,
	properties: {
		itemNode: cc.Node,
		layout: cc.Node,
		bureau: cc.Label,
	},

	onLoad: function () {
		this.curIndex = 0;
		this.list = this.dialogParameters.list;
		this.refrushLayout();
	},

	refrushLayout: function () {
		this.bureau.string = this.curIndex+1;
		let dataArray = this.list[this.curIndex];
		for (let node of this.layout.children) {
			node.destroy();
		}
		for (let i = 0; i < dataArray.length; ++i) {
			let data = dataArray[i];
			let node = cc.instantiate(this.itemNode);
			node.active = true;
			cc.find('bg1', node).active = (i%2 == 0);
			cc.find('bg2', node).active = (i%2 == 1);
			for (let i = 0; i < data.cards.length; ++i) {
				let cardNode = cc.find('card'+(i+1), node);
				Global.CCHelper.updateSpriteFrame('Game/Cards/'+data.cards[i], cardNode.getComponent(cc.Sprite));
			}
			let cardType = SZLogic.getCardsType(data.cards);
			let urls = ['', 'danzhang', 'duizi', 'shunzi', 'jinhua', 'shunjin', 'baozi'];
			Global.CCHelper.updateSpriteFrame('Game/Sanzhang/'+urls[cardType], cc.find('cardtype', node).getComponent(cc.Sprite));
			let stateNode = cc.find('state', node);
			if (data.winScore < 0) {
				let url = 'Game/Sanzhang/bipaishibai';
				if (data.isAbandon) {
					url = 'Game/Sanzhang/yiqipai';
				}
				Global.CCHelper.updateSpriteFrame(url, stateNode.getComponent(cc.Sprite));
			}
			else {
				stateNode.active = false;
			}
			cc.find('pour', node).getComponent(cc.Label).string = data.pourScore;
			if (data.winScore > 0) {
				let winNode = cc.find('win', node);
				winNode.active = true;
				winNode.getComponent(cc.Label).string = '+' + parseInt(data.winScore);
			}
			else {
				let loseNode = cc.find('lose', node);
				loseNode.active = true;
				loseNode.getComponent(cc.Label).string = parseInt(data.winScore);
			}
			Global.CCHelper.updateSpriteFrame(data.avatar, cc.find('avatar', node).getComponent(cc.Sprite));
			cc.find('name', node).getComponent(cc.Label).string = data.nickname;
			cc.find('banker', node).active = data.isBanker;
			node.parent = this.layout;
		}
	},

	onLeftClick: function () {
		-- this.curIndex;
		if (this.curIndex < 0) {
			this.curIndex = this.list.length-1;
		}
		this.refrushLayout();
	},

	onRightClick: function () {
		++ this.curIndex;
		if (this.curIndex >= this.list.length) {
			this.curIndex = 0;
		}
		this.refrushLayout();
	},

	onClose: function () {
		Global.DialogManager.destroyDialog('Game/Sanzhang/SZReview');
	},
});
